<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.icss.th.subjects.pojo.Subject"%>
<%@ page import="com.icss.th.subjects.service.SubjectService"%>
<%@ page import="com.icss.th.fulltext.pojo.SearchResult"%>
<%@page import="com.icss.th.common.Constants.SEARCH_TYPE"%>
<%@page import="com.icss.th.db.pojo.PageInfo"%>
<%
	String context = request.getContextPath();
	String spendTime = (String)request.getAttribute("spendTime");
	PageInfo pageInfo = (PageInfo)request.getAttribute("pageInfo");
	List<SearchResult> results = (List<SearchResult>)request.getAttribute("list");
	
	String keyword = (String)request.getAttribute("keyword");
	String subjectId = (String)request.getAttribute("subjectId");
	String type = (String)request.getAttribute("type");
	if("".equals(type) || type == null)	type = SEARCH_TYPE.QUESTION;
	String unkeyword = (String)request.getAttribute("unkeyword");
	unkeyword = unkeyword == null? "" : unkeyword;
	Integer startYear = (Integer)request.getAttribute("startYear");
	startYear = startYear == null? 0 : startYear;
	Integer endYear = (Integer)request.getAttribute("endYear");
	endYear = endYear == null? 0 : endYear;
%>
<!DOCTYPE html>
<html>
<head>
<title>题海</title>
<meta charset="UTF-8">
<link href="includes/css/style.css" rel="stylesheet" type="text/css">
<link href="includes/css/jquery-ui.min.css" rel="stylesheet" type="text/css">
<link href="includes/css/PagerView.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--弹出窗口开始-->
	<%
		Subject root = SubjectService.getRoot();
			if (root != null)
			{
		List<Subject> list = root.getChildren();
		if (list  != null && !list.isEmpty())
		{
	%>
	<div id="tabs" style="display: none">
		<div id="tabs0" style="position: absolute;">
			<ul class="menu0" id="menu0">
			<%
				for (int i = 0; i < list.size(); i++)
					{
						Subject level1 = list.get(i);
			%>
				<li onClick="setSubjectTab(<%=i%>)" class="<%=i == 0? "hover" : ""%>">&nbsp;<%=level1.getName()%></li>
			<%
				}
			%>
			</ul>
		</div>
		<div class="main scrollbar" id="main0" style="position: absolute;">
			<%
				for (int i = 0; i < list.size(); i++)
					{
						Subject level1 = list.get(i);
						List<Subject> list2 = level1.getChildren();
						if (list2  != null && !list2.isEmpty())
						{
			%>
			<ul class="<%=i == 0? "block" : ""%>">
			<%
				for (Subject level2 : list2)
					{
			%>
				<li><a href="#" id="<%=level2.getId()%>" onclick="setSubject(this, 'isSubmit')"><%=level2.getName()%></a></li>
			<%
				}
			%>
			</ul>
			<%
				}}
			%>
		</div>
	</div>
	<%
		}
			}
	%>
<!--弹出窗口结束-->

<div id=wrapper>
		<input type="hidden" name="pnum" id="pnum" value="">
		<input type="hidden" name="type" id="type" value="<%=type%>">
		<input type="hidden" name="keyword" id="keyword" value="<%=keyword%>">
		<input type="hidden" name="unkeyword" id="unkeyword" value="<%=unkeyword%>">
		<input type="hidden" name="startYear" id="sYear" value="<%=startYear%>">
		<input type="hidden" name=endYear id="eYear" value="<%=endYear%>">
	    <div id=content1>
	        <div id=u  class="top-bg">
	            <div id=user>
	                <span style="float:left; padding-left:20px; padding-top:2px;"><img src="includes/images/title.png" width="71" height="34"></span>
	                <span style="float:left; padding-left:40px; padding-top:8px;"><a href="<%=context%>/"><img src="includes/images/home.png" width="25" height="23"></a></span>
	                <div class="input-bg">
	                	<input id="keyword2" x-webkit-speech class=top_input >
                		<a href="#" onclick="_search()"><img style="margin-top:3px;" src="includes/images/seek.png" width="23" height="20"></a>
	                </div>
	                <div id=userMenu>
	                <a href="#">登录</a>&nbsp;<font color="#f68d00" size="2">|</font>&nbsp;
	                <a href="#">快速入口</a>&nbsp;<font color="#f68d00">|</font>&nbsp;
	                <a href="#">合作接口</a>
	                </div>
	            </div>
	        </div>
	    </div>
		<div id=m_list>
	        <div style="padding-left:10px; margin-left:120px; margin-bottom:3px; height:30px; float:left;" id="select">
	            <ul>
	                <li class="ul-li"><A id="typeOption1" href="#" onclick="optionSubmit('type', '1')">试题</A></li>
	                <li class="ul-li"><A id="typeOption2" href="#" onclick="optionSubmit('type', '2')">试卷</A></li>
	            </ul>
	        </div>
	        <div style="padding-left:10px; margin-left:40px; margin-bottom:3px; height:30px; float:left;">
	        <input type="hidden" name="subjectId" id="subjectId" value="<%=subjectId%>">
	        	<ul>
	        		<li id="subjectName" style="font-size:17px; font-weight:bold; height:40px; line-height:30px; color:#666; float:left;"></li>
	                <li class="ul-li" style=" margin-left:13px;"><a href="#" class="s_botton1" onClick="choseSubject();">检索题库</a></li>
	        	</ul>
	        </div>    
		</div>

	    <div id="list">
			<ul>
	            <li>找到约<%=pageInfo.getRecordCount()%>条结果 （用时<%=spendTime%>秒）</li>
	        </ul>
	        <%
	        	if (results != null && !results.isEmpty()){
	        	        	String source = "question";
	        	        	if ("2".equals(type)) source = "paper";
	        	        	for (SearchResult r : results){
	        %>
	        <ul>
	            <li><A style="font-family:arial" href="<%=context%>/<%=source%>?id=<%=r.getId()%>" class="list-title"><%=r.getTitle()%></A></li>
	            <li><A style="font-family:arial" href="#" style="color:#090; line-height:20px;"><%=r.getSourceName()%></A></li>
	            <li style="font-family:arial"><%=r.getContent()%></li>
	        </ul>
	        <%
	        	}
	        	        }
	        %>
	        
	        <div id="pager" style="height:30px; margin-bottom:20px; text-align:left;">
	        </div>
	        <ul style="height:30px; margin-bottom:1px; text-align:left;">
	            <li><A href="#" onclick="AddFavorite()">加入收藏夹</A>&nbsp;&nbsp;|&nbsp;&nbsp;<A href="#">关于我们</A>&nbsp;&nbsp;|&nbsp;&nbsp;<A href="#">加入我们</A>&nbsp;&nbsp;|&nbsp;&nbsp;<A href="#">支持与捐赠</A>&nbsp;&nbsp;|&nbsp;&nbsp;<A href="#">开放接口</A></li>
	            <li>©2013&nbsp;Tihai.org&nbsp;北京题海科技有限公司&nbsp;<A href="#" >使用必读</A>&nbsp;<a href="#" target="_blank">冀ICP备08007478号</a></li>
	        </ul>
	    </div>
</div>
<script src="includes/js/jquery-1.10.0.min.js" type="text/javascript"></script>
<script src="includes/js/jquery-ui.min.js" type="text/javascript"></script>
<script src="includes/js/PagerView.js" type="text/javascript"></script>
<script type="text/javascript">
	$("#keyword2").keypress(function(event){
		if ( event.which == 13 ) {
			_search();
		}
	});

	$(document).ready(function() {
		var pager = new PagerView('pager');
		pager.itemCount = <%=pageInfo.getRecordCount()%>;
		pager.size = <%=pageInfo.getCountPerPage()%>;
		pager.index = <%=pageInfo.getPageNum()%>
		pager.render();
		pager.onclick = function(index){
			$('#pnum').val(index);
			_submit();
		};
		
		
		if($('#type').val() == '1'){
			$('#typeOption1').addClass('curr');
			$('#typeOption2').removeClass('curr');
		}else if($('#type').val() == '2'){
			$('#typeOption1').removeClass('curr');
			$('#typeOption2').addClass('curr');
		}
		
		$('#subjectId').val(<%=subjectId%>);
		$('#subjectName').html('<%=SubjectService.getSubjectName(subjectId)%>');
		
	});
	
	
	function optionSubmit(option, value){
		if(option == 'type'){
			$('#pnum').val(1);
			$('#type').val(value);
			_submit();
		}else if(option == 'subject'){
			$('#pnum').val(1);
			_submit();
		}
	}
	
	function _submit(){
		if ($.trim($("#keyword").val())){
			var path = '<%=context%>/adsearch?';
			path += 'subjectId=' + $("#subjectId").val();
			path += '&type=' + $("#type").val();
			path += '&keyword=' + encodeURI(encodeURI($("#keyword").val()));
			path += '&unkeyword=' + encodeURI(encodeURI($("#unkeyword").val()));
			path += '&sYear=' + $("#sYear").val();
			path += '&eYear=' + $("#eYear").val();
			path += '&pnum=' + $("#pnum").val();
			location.href = path;
		}else{
			location.reload();
		}
	}
	
	function _search(){
		if ($.trim($("#keyword2").val())){
			var path = '<%=context%>/search?';
			path += 'subjectId=' + $("#subjectId").val();
			path += '&keyword=' + encodeURI(encodeURI($("#keyword2").val()));
			location.href = path;
		}else{
			location.reload();
		}
	}

	//弹出试题科目选择框
	function choseSubject(){
		$("#tabs").dialog({
			modal: true,
			height: 500,
			width: 700,
			resizable: false,
			draggable: false,
			title: '选择您需要的科目'
		});
	}

	//切换科目选择tab页
	function setSubjectTab(n){
		$("li", $("#menu0")).each(function(i, o){
			o.className = i == n? "hover" : "";
		});
		
		$("ul", $("#main0")).each(function(i, o){
			o.style.display = i == n? "block" : "none";
		});
	}

	//选定一个科目
	function setSubject(li, isSubmit){
		$("#subjectId").val(li.id);
		$("#subjectNameH").val(li.innerHTML);
		$("#subjectName").html(li.innerHTML);
		$("#tabs").dialog( "close" );
		
		if(isSubmit == 'isSubmit'){
			optionSubmit('subject', '');
		}
	}
	
	function AddFavorite(){
		var sURL = window.location;
		var sTitle = document.title;
	    try{
	        window.external.addFavorite(sURL, sTitle);
	    }catch (e){
	        try{
	            window.sidebar.addPanel(sTitle, sURL, "");
	        }catch (e){
	            alert("加入收藏失败，请使用Ctrl+D进行添加");
	        }
	    }
	}
</script>
</body>
</html>
